﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace PhotoDownloader
{
    class CodeAnalyzer
    {
        public static string DownloadCode(string url)
        {
            WebClient client = new WebClient();
            string htmlCode = client.DownloadString(url).ToLower();

            //Console.WriteLine(htmlCode);
            return htmlCode;
        }

        /// <summary>
        /// Find strings which are between defined phrases.
        /// </summary>
        /// <param name="text">Text to searching.</param>
        /// <param name="phrasesStart"></param>
        /// <param name="phrasesEnd"></param>
        /// <returns>List of results</returns>
        public static List<string> FindPhrases(string text, string phraseStart, string phraseEnd)
        {
            List<string> results = new List<string>();

            while (text.Contains(phraseStart))
            {
                int start = text.IndexOf(phraseStart) + phraseStart.Length;
                int end = text.IndexOf(phraseEnd, start);

                string result = text.Substring(start, end - start);

                results.Add(result);    // important line

                text = text.Substring(end);
            }

            return results;
        }

        /// <summary>
        /// Find defined phrase (phrase can be substring)
        /// </summary>
        /// <param name="text"></param>
        /// <param name="phrase"></param>
        /// <param name="phraseEnd"></param>
        /// <returns></returns>
        public static List<string> FindDefinedPhrases(string text, string phrase, 
            string phraseStart, string phraseEnd)
        {
            SaveToFile(text, "C:/out.txt");

            List<string> results = new List<string>();

            while (text.Contains(phrase))
            {
                Console.WriteLine("Hura, na pewno cos jest!");
                int start = FindCharacterOnTheLeft(text, phraseStart, text.IndexOf(phrase));
                Console.WriteLine("Index of phrase: " + text.IndexOf(phrase));
                Console.WriteLine("phrase: " + phrase + " IndexOf: " + start);
                int end = 0;
                if (start != -1)
                {
                    end = text.IndexOf(phraseEnd, start + 1);
                    Console.WriteLine("End of phrase: " + end);
                    string result = text.Substring(start + 1, end - start - 1);
                    Console.WriteLine("Result: " + result);

                    Console.WriteLine();

                    results.Add(result);    // important line
                }

                text = text.Substring(end);
            }

            results = RemoveBadImages(ref results);

            return results;
        }

        private static int FindCharacterOnTheLeft(string text, string character, int position)
        {
            while (position >= 0)
            {
                //if (text.ElementAt(position).Equals(character))
                if (text.ElementAt(position).ToString().Equals(character))
                {
                    return position;
                }

                position--;
            }

            return -1;
        }

        private static void SaveToFile(string text, string outFile) {
            StreamWriter writer = new StreamWriter(outFile);
            writer.Write(text);
            writer.Close();

            Console.WriteLine("Saved text to file: " + outFile);
        }

        private static List<string> RemoveBadImages(ref List<string> images)
        {
            Console.WriteLine("Start Usuwania: " + images.Count);
            for (int i = 0; i < images.Count; )
            {
                string img = images.ElementAt(i);
                Console.WriteLine("Bede usuwal: " + img);

                if (img.StartsWith("http"))
                {
                    images.RemoveAt(images.IndexOf(img));
                    //    break;
                }
                else if (img.Contains("head_rsk"))
                {
                    images.Remove(img);
                    //    break;
                }
                else if (!img.Contains("/"))
                {
                    images.Remove(img);
                }
                else if (img.Contains("header"))
                {
                    images.Remove(img);
                }
                else if (img.StartsWith("this.src"))
                {
                    images.Remove(img);
                    //    break;
                }
                else if (img.Contains("logo"))
                {
                    images.Remove(img);
                    //    break;
                }
                else if (img.Contains("Thumb"))
                {
                    images.Remove(img);
                    //    break;
                }
                else if (img.Contains("thumb"))
                {
                    images.Remove(img);
                    //    break;
                }
                else if (img.Contains(" "))
                {
                    images.Remove(img);
                    //    break;
                }
                else
                {
                    i++;
                }
            }
            Console.WriteLine("Finish usuwania: " + images.Count);
            return images;
        }
    }
}
